package com.main.hotel.service.impl;

import com.main.hotel.entity.OrderInfo;
import com.main.hotel.repository.OrderInfoRepository;
import com.main.hotel.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Service
public class OrderInfoServiceImpl implements OrderInfoService {
  @Autowired
  private OrderInfoRepository orderInfoRepository;

  @Override
  public Page<OrderInfo> getAllOrder(int pageNum, int pageSize,int customer_id,String time) {
    Sort sort=new Sort(Sort.Direction.DESC,"createTime");
    Specification specification = new Specification() {
      @Override
      public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
        Predicate predicate = cb.conjunction();
        System.out.println(customer_id);
        predicate.getExpressions().add(cb.equal(root.get("foo"), customer_id));
        predicate.getExpressions().add(cb.notEqual(root.get("order_status"),3));
//起始日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        String startTime="";
        if (time.equals("week")) {
          Calendar c=Calendar.getInstance();
          c.setTime(new Date());
          c.add(Calendar.DATE,-7);
          Date d=c.getTime();
          startTime=format.format(d);
          predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), startTime));
        }
        if (time.equals("month")) {
          Calendar c=Calendar.getInstance();
          c.setTime(new Date());
          c.add(Calendar.MONTH,-1);
          Date d=c.getTime();
          startTime=format.format(d);
          predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), startTime));
        }
        if (time.equals("halfYear")) {
          Calendar c=Calendar.getInstance();
          c.setTime(new Date());
          c.add(Calendar.MONTH,-6);
          Date d=c.getTime();
          startTime=format.format(d);
          predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), startTime));
        }
////结束日期
//        if (endTime != null && !endTime.trim().equals("")) {
//          predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), endTime));
//        }
        return predicate;
      }
    };

    Pageable pageable=PageRequest.of(pageNum,pageSize,sort);
    return orderInfoRepository.findAll(specification,pageable);
  }

//  @Override
//  public Page<OrderInfo> getAllOrder(int pageNum, int pageSize, String startTime) {
//    return null;
//  }

  @Override
  public OrderInfo getByOrderName(String name) {
    OrderInfo orderInfo= orderInfoRepository.findByOrderName(name);
    return orderInfo;
  }

  @Override
  public int cancelOrder(String order_id) {
    int result=orderInfoRepository.cancelOrder(order_id);
    return result;
  }

  @Override
  public int updateOrder(String order_id) {
    return orderInfoRepository.updateOrder(order_id);
  }
}
